package com.kyp.factory;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.util.GenericOptionsParser;
import org.apache.hadoop.util.Tool;

import com.kyp.basic.BasicCalc;
import com.kyp.util.KYPLog;

public class KYPMapReduceStore  extends Configured implements Tool{
	
	@Override
	public int run(String[] args) throws Exception {

		String[] otherArgs = new GenericOptionsParser(getConf(), args)
				.getRemainingArgs();
		
		if(otherArgs.length < 2){
			StringBuilder errSb = new StringBuilder();
			errSb.append("******************Usage********************\n");
			errSb.append("You Should Input <Input Folder> <Output Folder>\n");
			errSb.append("hadoop jar packagename.jar packagename.MainFunction -D <ParamKey>=<ParamValue> <Input Folder> <Output Folder>\n");
			errSb.append("-------------------------------------------\n");
			errSb.append("For Help \n");
			errSb.append("ParamKey : help, ParamValue : yes \n");
			errSb.append("******************Usage********************\n");			
			KYPLog.writeLog(KYPLog.ERROR_LOG, errSb.toString(), KYPMapReduce.class);
			
			System.exit(-1);
		}
		
		
		if(getConf().get("help") != null && getConf().get("help").equals("yes")){
			writeHelp();
			System.exit(0);
		}
		
		createMapReduce(getConf(), otherArgs);
		
		return 0;
	}
	
	public void createMapReduce(Configuration conf, String[] otherArgs){
		KYPMapReduce exeMapReduce; 
				
		switch (conf.get("pgm")) {
		case "basic":
			exeMapReduce = new BasicCalc();
			break;
		default:
			writeHelp();
			return;
		}
			
		exeMapReduce.execute(conf, otherArgs);
	}
	
	private void writeHelp(){
		StringBuilder infoSb = new StringBuilder();
		infoSb.append("******************Usage********************\n");
		infoSb.append("hadoop jar packagename.jar packagename.MainFunction -D <ParamKey>=<ParamValue> <Input Folder> <Output Folder>");
		infoSb.append("-------------------------------------------\n");
		infoSb.append("For Help \n");
		infoSb.append("ParamKey : help, ParamValue : yes \n");
		infoSb.append("----------------- PROGRAM -----------------\n");
		infoSb.append("////////////////////////////////////////////\n");
		infoSb.append("1. BASIC CALUCATOR\n");
		infoSb.append("ParamKey : pgm, ParamValue : basic\n");
		infoSb.append("※ Required Parameter\n");
		infoSb.append("ParamKey : group, ParamValue : 3 (Group Left Column Count)\n");
		infoSb.append("////////////////////////////////////////////\n");
		infoSb.append("******************Usage********************\n");
		KYPLog.writeLog(KYPLog.INFO_LOG, infoSb.toString(), KYPMapReduce.class);
	}
}
